/* serverprog.c - a stream socket server demo */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include "sine_socket.h"
 
/* the port users will be connecting to */
#define MYPORT 3490
/* how many pending connections queue will hold */
#define BACKLOG 10
#define MAXDATASIZE 300
 
void sigchld_handler(int s)
{
    while(wait(NULL) > 0);
}
 
int main(int argc, char *argv[ ])
{
/* listen on sock_fd, new connection on new_fd */
int sockfd, new_fd, numbytes;
char buf[MAXDATASIZE];
/* my address information */
struct sockaddr_in my_addr;
/* connector’s address information */
struct sockaddr_in their_addr;
int sin_size;
struct sigaction sa;
int yes = 1;
 
if ((sockfd = sine_socket(AF_INET, SOCK_STREAM, 0, 12012)) == -1)
{
    perror("Server-socket() error lol!");
    exit(1);
}
else
printf("Server-socket() sockfd is OK...\n");
 
if (sine_setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
{
    perror("Server-setsockopt() error lol!");
    exit(1);
}
else
    printf("Server-setsockopt is OK...\n");
 
/* host byte order */
my_addr.sin_family = AF_INET;
/* short, network byte order */
my_addr.sin_port = htons(MYPORT);
/* automatically fill with my IP */
my_addr.sin_addr.s_addr = INADDR_ANY;
 
printf("Server-Using %s and port %d...\n", inet_ntoa(my_addr.sin_addr), MYPORT);
 
/* zero the rest of the struct */
memset(&(my_addr.sin_zero), '\0', 8);
 
if(sine_bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
{
    perror("Server-bind() error");
    exit(1);
}
else
    printf("Server-bind() is OK...\n");
 
if(sine_listen(sockfd, BACKLOG) == -1)
{
    perror("Server-listen() error");
    exit(1);
}
printf("Server-listen() is OK...Listening...\n");
 
/* clean all the dead processes */
sa.sa_handler = sigchld_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
 
if(sigaction(SIGCHLD, &sa, NULL) == -1)
{
    perror("Server-sigaction() error");
    exit(1);
}
else
    printf("Server-sigaction() is OK...\n");
 
/* accept() loop */
while(1)
{
sin_size = sizeof(struct sockaddr_in);
if((new_fd = sine_accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1)
{
    perror("Server-accept() error");
    continue;
}
else
    printf("Server-accept() is OK...\n");
printf("Server-new socket, new_fd is OK...\n");
printf("Server: Got connection from %s\n", inet_ntoa(their_addr.sin_addr));
 
/* this is the child process */
if(!fork())
{
    /* child doesn’t need the listener */
   sine_close(sockfd);
   
   if(sine_send(new_fd, "This is a test string from server!\n", 37, 0) == -1)
        perror("Server-send() error lol!");
 
   if((numbytes = recv(new_fd, buf, MAXDATASIZE-1, 0)) == -1)
	{
	    perror("recv()");
	    exit(1);
	}
    else
    	printf("Server-The recv() is OK...\n");

	buf[numbytes] = '\0';
	printf("Server-Received: %s", buf);
	sine_close(new_fd);
	exit(0);
}
else
    printf("Server-send is OK...!\n");
 
/* parent doesn’t need this*/
//sine_close(new_fd);
//printf("Server-new socket, new_fd closed successfully...\n");
}
return 0;
}
